﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace DreamBuilder.UI.UserControls
{
    public partial class AttachmentUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.uploadFile();
            }
        }

        public void uploadFile()
        {
            try
            {
                // 获取上传的文件信息
                HttpPostedFile file_upload = Request.Files["Filedata"];
                #region 附件实体增加更多信息 20110525
                Hashtable addInfos = new Hashtable();
                if (Session != null)
                {
                    addInfos = (Hashtable)Session["AddInfos"];
                }
                #endregion
                string extension = string.Empty;
                string fileName = string.Empty;
                //bool isImg = false;
                if (file_upload.ContentLength > 0)
                {
                    fileName = file_upload.FileName;
                    if (fileName.IndexOf(".") != -1)
                    {
                        extension = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - fileName.LastIndexOf(".") - 1).ToLower();
                    }
                    AttachmentUploadFile uf = new AttachmentUploadFile();
                    uf.AttachmentDBHelperString = Request.QueryString["AttachmentDBHelperString"];
                    int state = 0;
                    string attachmentID = Guid.NewGuid().ToString();
                    DateTime uploadTime = DateTime.Now;
                    string newFileName = uf.SaveFile(file_upload, ref state, attachmentID, uploadTime, addInfos);
                    //0:上传成功.  1:没有选择要上传的文件.  2:上传文件类型不符.   3:上传文件过大  -1:应用程序错误.
                    int statusCode = 500;
                    switch (state)
                    {
                        case 0:
                            statusCode = 200;
                            break;
                        case 1:
                            statusCode = 200;
                            break;
                        default:
                            statusCode = 500;
                            break;
                    }
                    
                    List<AttachmentUploadFileInfo> listufi = new List<AttachmentUploadFileInfo>();
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<AttachmentUploadFileInfo>));

                    listufi.Add(new AttachmentUploadFileInfo()
                    {
                        Id = listufi.Count,
                        FileName = newFileName,
                        AttachmentID = attachmentID,
                        AttachmentSize = file_upload.ContentLength,
                        UploadTime = uploadTime.ToString("yyyy-MM-dd HH:mm:ss")
                    });
                    string postData = "";
                    //序列化
                    using (MemoryStream stream = new MemoryStream())
                    {
                        serializer.WriteObject(stream, listufi);
                        postData = Encoding.UTF8.GetString(stream.ToArray());
                    }

                    Response.StatusCode = statusCode;
                    Response.Write(postData);
                }
            }
            catch (Exception e)
            {
                //内部服务器错误
                Response.StatusCode = 500;
                Response.Write("内部服务器错误:" + e.Message.ToString());
            }
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
}